"""
主要是顶部工具栏
包含顶部功能元素和cd到对应页面的函数
"""

import sys
import time
from Page.BasePage import BasePage
from utils.log import logger


class HomePage(BasePage):
    # 配置文件读取元素
    cms_url = BasePage.cf.getLocatorsOrAccount('LoginAccount', 'cms_url')
    cms_username = BasePage.cf.getLocatorsOrAccount('LoginAccount', 'cms_username')
    cms_password = BasePage.cf.getLocatorsOrAccount('LoginAccount', 'cms_password')

    username = BasePage.cf.getLocatorsOrAccount('LoginPage', 'username')
    password = BasePage.cf.getLocatorsOrAccount('LoginPage', 'password')

    loginBtn = BasePage.cf.getLocatorsOrAccount('LoginPage', 'loginBtn')

    Dashboard = BasePage.cf.getLocatorsOrAccount('HomePage', 'Dashboard')
    Device = BasePage.cf.getLocatorsOrAccount('HomePage', 'Device')
    Alarm = BasePage.cf.getLocatorsOrAccount('HomePage', 'Alarm')
    Monitor = BasePage.cf.getLocatorsOrAccount('HomePage', 'Monitor')
    Admin = BasePage.cf.getLocatorsOrAccount('HomePage', 'Admin')

    language = BasePage.cf.getLocatorsOrAccount('HomePage', 'lange_menu')
    lange_english = BasePage.cf.getLocatorsOrAccount('HomePage', 'lange_english')

    # 系统管理页下的侧边栏
    Enterprise = BasePage.cf.getLocatorsOrAccount('Admin', 'Enterprise')
    Group = BasePage.cf.getLocatorsOrAccount('Admin', 'Group')
    Users = BasePage.cf.getLocatorsOrAccount('Admin', 'Users')

    def login_cms(self):
        """登录, 并切换语言为英文"""
        print('-------staring login-------')
        self.loadUrl(self.cms_url)
        # time.sleep(2)
        self.sendKeys(*self.username, self.cms_username)
        self.sendKeys(*self.password, self.cms_password)

        self.click(*self.loginBtn)
        print('---------start change language---------')
        # time.sleep(2)

        # 切换英文
        self.findElement(*self.language).click()
        time.sleep(1)
        self.findElement(*self.lange_english).click()
        time.sleep(1)

        print('---------end login---------')

    def cd_Dashboard(self):
        self.click(*self.Dashboard)

    def cd_Device(self):
        self.click(*self.Device)

    def cd_Alarm(self):
        self.click(*self.Alarm)

    def cd_Monitor(self):
        self.click(*self.Monitor)

    def cd_Admin(self):
        self.click(*self.Admin)


if __name__ == "__main__":
    from selenium import webdriver

    options = webdriver.ChromeOptions()
    # 屏蔽自动化受控提示 && 开发者提示
    options.add_experimental_option("excludeSwitches", ['enable-automation', 'load-extension'])
    # 浏览器启动时窗口最大化
    options.add_argument("--start-maximized")
    # 屏蔽保存密码弹窗
    options.add_experimental_option("prefs", {"credentials_enable_service": False,
                                              "profile.password_manager_enabled": False})
    driver = webdriver.Chrome(options=options)

    home_page = HomePage(driver, 10)
    home_page.login_cms()
    print('login finish')

    home_page.cd_Admin()

    home_page.findElement(*home_page.Group).click()

    time.sleep(1111)
